package com.nanbei.a14;

import org.springframework.cglib.core.Signature;

/**
 * @ClassDescription:
 * @JdkVersion: 1.8
 * @Author: libs
 * @Created: 2024/11/18 17:43
 */
public class ProxyFastClass {
    static Signature signature0 = new Signature("saveSuper","()V");
    static Signature signature1 = new Signature("saveSuper","(I)V");
    static Signature signature2 = new Signature("saveSuper","(J)V");

    /**
     * 获取目标方法的编号
     * Proxy
     *      save()      0
     *      save(int)   1
     *      save(long)  2
     *
     * @param signature 方法的签名 (包括方法的名称，入参，返回值)
     * @return 编号
     */
    public int getIndex(Signature signature){
        if (signature.equals(signature0)){
            return 0;
        }else if (signature.equals(signature1)){
            return 1;
        }else if (signature.equals(signature2)){
            return 2;
        }else {
            return -1;
        }

    }


    // 根据 getIndex 方法返回的编号，正常调用目标对象方法
    public Object invoke(int index, Object proxy, Object[] args) {
        if (index == 0){
            ((Proxy)proxy).saveSuper();
            return null;
        }else if (index == 1){
            ((Proxy)proxy).saveSuper((int)args[0]);
            return null;
        }else if (index == 2){
            ((Proxy)proxy).saveSuper((int)args[1]);
            return null;
        }else {
            throw new RuntimeException("没有该方法，方法调用失败");
        }
    }

    public static void main(String[] args) {
        ProxyFastClass proxyFastClass = new ProxyFastClass();
        int index = proxyFastClass.getIndex(new Signature("saveSuper", "(I)V"));
        System.out.println(index);
        proxyFastClass.invoke(index,new Proxy(),new Object[]{100});
    }


}
